#include "servicecamera.h"

namespace infinity {

/*static*/ const ServiceKey ServiceCamera::Key = ServiceKey("Service::Camera");

ServiceCamera::ServiceCamera()
:	super()
,	m_activeCam(NULL)
{
	m_debugCam = new CameraDebug();
	m_fpsCam = new CameraFps();

	setActiveCameraController(CAMERA_DEBUG);
}

/*virtual*/ ServiceCamera::~ServiceCamera()
{
}

/*virtual*/ void ServiceCamera::onUpdateMedia(dw::float32 deltaT)
{
	if (m_activeCam != NULL)
	{
		m_activeCam->update(deltaT);
	}
}
void ServiceCamera::bindPhysicsObject(physics::Object* physicsObject)
{
	if (m_activeCam != NULL)
	{
		m_activeCam->bindPhysicsObject(physicsObject);
	}
}

void ServiceCamera::bindEntity(Entity3D* entity)
{
	if (m_activeCam != NULL)
	{
		m_activeCam->bindEntity(entity);
	}
}

Service::State ServiceCamera::onInitializeMedia()
{
	return super::onInitializeMedia();
}

void ServiceCamera::setActiveCameraController(CameraType type)
{
	if (m_activeCam != NULL)
	{
		m_activeCam->onUnRegistered();
	}

	switch (type)
	{
	case CAMERA_DEBUG :
		m_activeCam = m_debugCam;
		break ;
	case CAMERA_FPS :
		m_activeCam = m_fpsCam;
		break ;
	default :
		m_activeCam = m_debugCam;
		break ;
	}

	if (m_activeCam != NULL)
	{
		m_activeCam->onRegistered();
	}
}

Service::State ServiceCamera::onTerminateMedia()
{
	State selfState = State_Terminating;

	if (m_debugCam != NULL)
	{
		delete m_debugCam;
		delete m_fpsCam;

		selfState = State_Terminated;
	}

	if (selfState == State_Terminated)
	{
		return super::onTerminateMedia();
	}

	return selfState;
}

} // namespace infinity
